﻿namespace DesignModeConsole.行为型.职责链模式.处理不返回;

/// <summary>
/// 处理后继续往下执行
/// </summary>
public abstract class Handler3
{
    private Handler3 Successor;

    public void SetSuccessor(Handler3 successor)
    {
        Successor = successor;
    }

    public void Handle()
    {
        DoHandle();
        if (Successor is not null)
        {
            Successor.Handle();
        }
    }

    protected abstract void DoHandle();
}

public class HandlerA3 : Handler3
{
    protected override void DoHandle()
    {
        // 业务逻辑处理
        Console.WriteLine("handlerA3 业务逻辑处理");
    }
}

public class HandlerB3 : Handler3
{
    protected override void DoHandle()
    {
        // 业务逻辑处理
        Console.WriteLine("handlerB3 业务逻辑处理");
    }
}

public class HandlerChain3
{
    /// <summary>
    /// 链表头
    /// </summary>
    private Handler3 head;

    /// <summary>
    /// 链表尾
    /// </summary>
    private Handler3 tail;

    public void AddHandler(Handler3 handler3)
    {
        handler3.SetSuccessor(null);
        if (head is null)
        {
            head = handler3;
            tail = handler3;
        }

        // 将当前处理器类给链表尾的下一个处理
        tail.SetSuccessor(handler3);
        // 将当前处理器类赋值给链表尾
        tail = handler3;
    }

    public void Handle()
    {
        if (head != null)
        {
            head.Handle();
        }
    }
}